﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hotnet.Entity;
using Hotnet.Dal.BaseRepository;
using Hotnet.Entity.Search;
using System.Linq.Expressions;
using Hotnet.Entity.Enum;
using Hotnet.Dal.Utility;

namespace Hotnet.Dal.NSDalCommon
{
	public class DalTag : Repository<Tag>
	{
		public Tag[] List(int pageNo, int pageSize, out int totalCount, SearchTagCondition searchObj)
		{
			string tagCountCondtion = "";
			string tagNameCondition = "";
			string targetTypeCondition = "";
			int from = pageNo * pageSize+1;
			int to = (pageNo+1) * pageSize ;

			if (searchObj.TagCountType!=null&&searchObj.TagCountType == (int)EnumTagCountType.引用次数)
				tagCountCondtion += " RefCount ";
			else
				tagCountCondtion += " ClickCount ";
			if (!string.IsNullOrEmpty(searchObj.TagName))
				tagNameCondition = string.Format(" and TagName like '%{0}%' ", searchObj.TagName);
			if (searchObj.TargetType != null)
				targetTypeCondition = string.Format(" and TargetType ={0} ",searchObj.TargetType.Value);

			string sql = string.Format(@"
						SELECT TagID
						,TagName
						,ClickCount
						,RefCount
						,TargetType
						,AddTime
					FROM 
					(SELECT ROW_NUMBER() OVER (ORDER BY {0} desc)AS Row, * FROM Tag 
						where
						1=1 
						{1} 
						{2} 
					) as list
					WHERE Row between {3} and {4}", tagCountCondtion, tagNameCondition, targetTypeCondition, from, to);
			string sqlCount = string.Format(@"
											SELECT Count(*) 
											FROM 
											(SELECT ROW_NUMBER() OVER (ORDER BY {0} desc)AS Row, * FROM Tag 
												where
												1=1 
												{1} 
												{2} 
											) as list
											", tagCountCondtion, tagNameCondition, targetTypeCondition);
			totalCount = disableCacheDataContext.ExecuteQuery<int>(sqlCount).FirstOrDefault();
			Tag[] list=disableCacheDataContext.ExecuteQuery<Tag>(sql).ToArray();

			return list;
		}

		/// <summary>
		/// 列出热门点击Tag
		/// </summary>
		/// <param name="number"></param>
		/// <param name="tagType"></param>
		/// <returns></returns>
		public Tag[] ListClickHotTags(int number,EnumTagType tagType)
		{
			return disableCacheDataContext.Tag.Where(x => x.TargetType == (int)tagType).Take(number).OrderByDescending(x => x.ClickCount).ToArray();
		}

		/// <summary>
		/// 列出热门引用的Tag
		/// </summary>
		/// <param name="number"></param>
		/// <param name="tagType"></param>
		/// <returns></returns>
		public Tag[] ListReferenceHotTag(int number,EnumTagType tagType)
		{
			return disableCacheDataContext.Tag.Where(x => x.TargetType == (int)tagType).Take(number).OrderByDescending(x => x.RefCount).ToArray();;
		}

		/// <summary>
		/// 更新tag的引用次数
		/// </summary>
		/// <param name="tagIDs"></param>
		/// <param name="tagType"></param>
		public void UpdateTagsReferenceCount(int[] tagIDs, EnumTagType tagType)
		{
			string sql = string.Format("update tag set RefCount=RefCount+1 where TagID in {0} and TargetType={1}", SqlUility.ConvertArrayToInStatement(tagIDs), (int)tagType);
			dataContext.ExecuteCommand(sql);
		}

		/// <summary>
		/// 更新tag的click次数
		/// </summary>
		/// <param name="tagIDs"></param>
		/// <param name="tagType"></param>
		public void UpdateTagsClickCount(int[] tagIDs, EnumTagType tagType)
		{
			string sql = string.Format("update tag set ClickCount=ClickCount+1 where TagID in {0} and TargetType={1}", SqlUility.ConvertArrayToInStatement(tagIDs), (int)tagType);
			dataContext.ExecuteCommand(sql);
		}

	}
}
